var DoctorOperation = {
    id: "DoctorOperationTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

var currentSeat = parent.parent.$('#currentSeat').html();  //选定界面
DoctorOperation.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', width: 120,
            formatter: function (value, row, index) {
                if (currentSeat.indexOf("到店") !== -1 ) {
                    return "<a href='javascript:;' onclick = 'DoctorOperation.openBileeDD(" + '"' + row.customerId + '"' + ")' style='display: flex;align-items: center;'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }else if(currentSeat.indexOf("TC") !== -1 ){
                    return "<a href='javascript:;' onclick = 'DoctorOperation.consultantToDetailTC(" + '"' + row.customerId + '"' + ")' style='display: flex;align-items: center;'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }else if(currentSeat.indexOf("医生") !== -1 ){
                    return "<a href='javascript:;' onclick='DoctorOperation.openBileeYS(" + '"' + row.customerId + '"' + ")' style='display: flex;align-items: center;'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
                }
            }
        },
        {title: '联系电话', field: 'customerPhone', align: 'center', valign: 'middle', width: 130,},
        {title: '性别', field: 'customerSex', align: 'center', valign: 'middle', width: 100,
            formatter: function (value, row, index) {
                return value === 1 ? '男' : value === 2 ? '女' : '未知';
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', width: 100,},
        {title: '门店', field: 'store', align: 'center', valign: 'middle', width: 130,},
        {title: '时间', field: 'operationTime', align: 'center', valign: 'middle', width: 170,},
        {title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true,width: 100,
            formatter: function (value, row, index) {
                return '<button title="删除" onclick="DoctorOperation.deleteOperationRecords(\'' + row.id + '\',\'' + row.customerName + '\')"" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>'
            }
        }
    ];
    return columns;
};

DoctorOperation.openBileeDD = function (id) {//点击姓名查看详情 到店
    $.ajax({
        url: Feng.ctxPath + "/consultantTwo/getdcsByCustomerId",
        type: "post",
        data: {
            "id": id
        },
        success: function (data) {
            if (data == 1) {
                var index = layer.open({
                    type: 2,
                    title: '到店客户详情',
                    area: ['100%', '100%'],
                    fix: true,
                    maxmin: true,
                    content: Feng.ctxPath + '/customerDetails/to_openBileLists/' + id + "/0"
                });
                this.layerIndex = index;
            }else{
                alert("请先联系前台客服匹配建档！");
                return;
            }
        }
    });
};

DoctorOperation.deleteOperationRecords = function (id, customerName) {//删除操作记录
    var r = confirm("是否删除患者：" + customerName + " 的操作记录？")
    if (r == true) {
        var ajax = new $ax(Feng.ctxPath + "/doctorLooked/deleteOperationRecords/" + id, function (data) {
            Feng.success("删除成功!");
            DoctorOperation.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
}

DoctorOperation.consultantToDetailTC = function (id) {//点击姓名查看详情 TC
    var index = layer.open({
        type: 2,
        title: '&nbsp',
        area: ['100%', '100%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/consultantDetails/consultantToDetailByTab/' + id + '/0/0'
    });
    this.layerIndex = index;
};

DoctorOperation.openBileeYS = function (id) {//点击姓名查看详情 医生
    var index = layer.open({
        type: 2,
        title: '患者信息',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorDetails/doctor_seeDiagnosis/' + id + '/' + 0
    });
    this.layerIndex = index;
};

DoctorOperation.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        DoctorOperation.seItem = selected[0];
        return true;
    }
};

DoctorOperation.search = function () {//查询列表
    DoctorOperation.table.refresh({query: DoctorOperation.formParams()});
};

DoctorOperation.formParams = function () {
    var queryData = {};
    queryData['operationTime'] = $("#operationTime").val();
    queryData['userName'] = $("#userName", parent.document).val();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    queryData['outpatientName'] = $("#outpatientName", parent.document).val();
    return queryData;
};

DoctorOperation.inquire = function () {
    DoctorOperation.search();
};

DoctorOperation.resetSearch = function () {
    $("#operationTime").val("");
    DoctorOperation.inquire();
};

DoctorOperation.time = function () {
    var time = new Date();
    var month = time.getMonth() + 1;
    var day = time.getDate();
    if(month < 10){month = "0" + month}
    if(day < 10){day = "0" + day}
    $("#operationTime").val(time.getFullYear() + "-" + month + "-" + day);
    $("#revisitTimeTwo").val(time.getFullYear() + "-" + month + "-" + day);
};

DoctorOperation.timeChange = function (obj) {
    var time = $("#operationTime").val();
    var date = new Date(time);
    if(obj == '+'){
        if($("#operationTime").val().length == 7){
            date.setMonth(date.getMonth() + 1);
        }else if($("#operationTime").val().length == 10){
            date.setDate(date.getDate() + 1);
        }
    }else {
        if($("#operationTime").val().length == 7){
            date.setMonth(date.getMonth() - 1);
        }else if($("#operationTime").val().length == 10){
            date.setDate(date.getDate() - 1);
        }
    }
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if(month < 10){month = "0" + month}
    if(day < 10){day = "0" + day}
    $("#operationTime").val(date.getFullYear() + "-" + month + "-" + day);
    DoctorOperation.inquire();
};

DoctorOperation.sunMoon = function (obj) {
    var time = $("#operationTime").val();
    if(obj == 7){
        var moon = time.substring(0, 7);
        $("#operationTime").val(moon)
    }else if(obj == 10){
        $("#operationTime").val($("#revisitTimeTwo").val())
    }
    DoctorOperation.inquire();
};

$("#doctorSearchAll", parent.document).click(function(){  //主页搜索
    DoctorOperation.inquire();
});

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#DoctorOperationTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}
$(function () {
    DoctorOperation.time();
    var defaultColunms = DoctorOperation.initColumn();
    var table = new BSTable(DoctorOperation.id, "/doctorLooked/operationList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DoctorOperation.formParams());
    DoctorOperation.table = table.init();
    DoctorOperation.inquire();
    checkDate = function () {DoctorOperation.inquire();};
    $(document).on('click','#laydate_clear,#laydate_today,#laydate_ok',function () {DoctorOperation.inquire();});
    phonePrivileges();
});